<template>
    <div class="qingwu">
        <div class="admin_table_page_title">
            <a-button @click="$router.back()" class="float_right" icon="arrow-left">返回</a-button>
            电表添加
        </div>
        <div class="unline underm"></div>
        <div class="admin_form">
            <a-form-model :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
                <a-form-model-item label="电表名称">
                    <a-input v-model="info.name"></a-input>
                </a-form-model-item>
                <a-form-model-item label="电表资产编号">
                    <a-input v-model="info.code"></a-input>
                </a-form-model-item>
                <a-form-model-item label="初始值">
                    <a-input v-model="info.number"></a-input>
                </a-form-model-item>
                <a-form-model-item label="电表表地址">
                    <a-input v-model="info.tableAddress"></a-input>
                </a-form-model-item>
                <a-form-model-item label="倍率">
                    <a-input v-model="info.mult"></a-input>
                </a-form-model-item>
                <a-form-model-item label="设备层级">
                    <a-select v-model="info.level">
                        <a-select-option v-for="(v,k) in levelList" :key="k" :value="v.value"
                                         @click="chose_level(v.value)">{{ v.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item label="父级表">
                    <a-select v-model="info.electric_id">
                        <a-select-option v-for="(v,k) in electricList" :key="k" :value="v.value">{{ v.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item label="采集器">
                    <a-select v-model="info.collect_id">
                        <a-select-option v-for="(v,k) in collectList" :key="k" :value="v.value">{{ v.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item label="缴费方案">
                    <a-select v-model="info.charge_id">
                        <a-select-option v-for="(v,k) in planList" :key="k" :value="v.value">{{ v.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item label="报警方案">
                    <a-select v-model="info.alarm_id">
                        <a-select-option v-for="(v,k) in alarmList" :key="k" :value="v.value">{{ v.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item label="电表品牌">
                    <a-select v-model="info.brand">
                        <a-select-option v-for="(v,k) in brandList" :key="k" :value="v.value">{{ v.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item label="接入象限">
                    <a-select v-model="info.cate">
                        <a-select-option v-for="(v,k) in cateList" :key="k" :value="v.id">{{ v.name }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>

                <a-form-model-item :wrapper-col="{ span: 12, offset: 5 }">
                    <a-button type="primary" @click="handleSubmit">提交</a-button>
                </a-form-model-item>
            </a-form-model>

        </div>
    </div>
</template>

<script>
import moment from 'moment';

export default {
    components: {},
    props: {},
    data() {
        return {
            info: [],
            levelList: [],//设备层级
            brandList: [],//电表品牌
            collectList: [],//采集器
            planList: [],//缴费计划
            alarmList: [],//报警计划
            electricList: [],//父级列表
            cateList: [{id: 1, name: '单相表'}, {id: 2, name: '三相表'}],
            id: 0,
            loading: false,
        };
    },
    watch: {},
    computed: {},
    methods: {
        moment,
        handleSubmit() {
            // 验证代码处
            if (this.$isEmpty(this.info.name)) {
                return this.$message.error('电表名称不能为空');
            }
            if (this.$isEmpty(this.info.code)) {
                return this.$message.error('电表编号不能为空');
            }


            let api = this.$apiHandle(this.$api.estateElectricMeter, this.id);
            if (api.status) {
                this.$put(api.url, this.info).then(res => {
                    if (res.code == 200) {
                        this.$message.success(res.msg)
                        return this.$router.back();
                    } else {
                        return this.$message.error(res.msg)
                    }
                })
            } else {
                this.$post(api.url, this.info).then(res => {
                    if (res.code == 200) {
                        this.$message.success(res.msg)
                        return this.$router.back();
                    } else {
                        return this.$message.error(res.msg)
                    }
                })
            }


        },
        get_info() {
            this.$get(this.$api.estateElectricMeter + '/' + this.id).then(res => {
                this.info = res.data;
            })
        },
        // 获取菜单列表
        onload() {
            // 判断你是否是编辑
            if (!this.$isEmpty(this.$route.params.id)) {
                this.id = this.$route.params.id;
                this.get_info();

            }
            this.get_plan(1);
            this.$get(this.$api.estateElectricMeter, {per_page: 100}).then(res => {
                this.list = res.data.data;
            });

        },
        time_format(val) {
            this.info.adv_start = moment(val[0]._d).format("YYYY-MM-DD HH:mm:ss")
            this.info.adv_end = moment(val[1]._d).format("YYYY-MM-DD HH:mm:ss")
            this.$forceUpdate();
        },
        get_plan(type) {
            this.$get(this.$api.estateGetCommunity+"/"+type).then(res => {
                this.data = res.data;
                this.levelList = this.data.level_arr;//设备层级
                this.brandList = this.data.brand_arr//电表品牌
                this.planList = this.data.plan_arr//缴费计划
                this.alarmList = this.data.alarm_arr//报警计划
                this.collectList = this.data.collector_arr//采集器
            })

        },
        /**
         * 选择区域
         */
        chose_level(id) {
            this.electricList = [];
            this.data.electric_arr.forEach(item => {
                if (item.level == id) {
                    this.electricList.push(item);
                }
            })

            this.$forceUpdate();
        },
        /*
            选择分区
         */
        chose_area(id) {
            this.newTowerList = [];
            this.data.tower_arr.forEach(item => {
                if (item.area_id == id) {
                    this.newTowerList.push(item)
                }
            })
            this.$forceUpdate();
        }

    },

    created() {
        this.onload();
    },
    mounted() {
    }
};
</script>
<style lang="scss" scoped>

</style>
